namespace ghost {
    export class UIBase extends Laya.Box {
        public static lastPanel:UIBase[] = [];
        public static getLastPanelNearPos(panel:UIBase):Laya.Point
        {
            let pt:Laya.Point = Laya.Point.TEMP;
            let last:UIBase;
            if(UIBase.lastPanel.length > 0)
            {
                last = UIBase.lastPanel.pop();
                while((last.parent == null || last == panel) && UIBase.lastPanel.length > 0)            
                    last = UIBase.lastPanel.pop();            
            }            
            if(last && last.parent && last != panel)
            {
                let padding:number = 40;
                pt.x = last.x + padding + panel.width;
                pt.y = last.y + padding + panel.height;
                pt = (<Laya.Sprite>last.parent).localToGlobal(pt,false);                
                pt.x = pt.x > Laya.stage.width?last.x - panel.width - padding:(last.x + padding);                
                pt.y = pt.y > Laya.stage.height?last.y - panel.height - padding:(last.y + padding);
                return pt;
            }            
            return null;            
        }
        public back: Laya.Image;
        public titleLab: Laya.Text;
        public closeBtn: Laya.Image;
        public commitBtn: Laya.Button;
        protected _commitFunc: (...data) => void;
        constructor() {
            super();
            let s = this;
            s.width = 800;
            s.height = 600;
            s.x = GameManager.DESIGN_WIDTH - s.width >> 1;
            s.y = GameManager.DESIGN_HEIGHT - s.height >> 1;
            s.back = new Laya.Image;
            s.back.skin = Conf.uiAlias+"codex_tanchuangdi.png";
            s.back.left = s.back.right = s.back.top = s.back.bottom = 0;
            s.back.sizeGrid = "100,100,100,100";
            s.addChild(s.back);
            s.titleLab = new Laya.Text;
            s.titleLab.fontSize = 30;
            s.titleLab.color = "#ff9900";
            s.titleLab.x = 10;
            s.titleLab.y = -6;
            s.titleLab.bold = true;
            s.titleLab.text = "对话框";
            s.titleLab.stroke = 4;
            s.titleLab.strokeColor = "#000000";
            s.addChild(s.titleLab);
            s.closeBtn = new Laya.Image;
            s.closeBtn.skin = Conf.uiAlias+"closeBtn.png";
            s.closeBtn.anchorX = s.closeBtn.anchorY = 0.5;
            s.closeBtn.scale(0.5, 0.5);
            s.addChild(s.closeBtn);
            s.commitBtn = s.createBtn("确定");
            s.addChild(s.commitBtn);
            s.closeBtn.on(Laya.Event.CLICK, s, s.oper);
            s.commitBtn.right = 20;
            s.commitBtn.bottom = 20;
            s.closeBtn.right = 0;
            s.closeBtn.top = -14;
            s.back.on(Laya.Event.MOUSE_DOWN, s, s.downBack);
            s.back.on(Laya.Event.MOUSE_UP, s, s.upBack);
        }
        public set title(val: string) {
            this.titleLab.text = val;
        }
        public get title(): string {
            return this.titleLab.text;
        }
        protected downBack(): void {
            let s = this;
            s.startDrag();
        }
        protected upBack(): void {
            let s = this;
            s.stopDrag();
        }
        protected oper(e: Laya.Event): void {
            let s = this;
            if (e.currentTarget == s.closeBtn) {
                s.hide();
            }
            else if (e.currentTarget == s.commitBtn) {
                s.commit();
            }
        }
        public commit(): void {
            let s = this;
            if (s._commitFunc)
                s._commitFunc(null);
        }
        public show(pr: Laya.Sprite = null, data: any = null, commit: (data: any) => void = null): void {
            let s = this;
            pr = pr == null ? Laya.stage : pr;
            s._commitFunc = commit;
            if (s.parent == null)
                Laya.stage.on(Laya.Event.RESIZE, s, s.resize);
            if (pr != s.parent) {
                pr.addChild(s);
            }
            s.resize();
            s.setData(data);
        }
        public hide(): void {
            let s = this;
            if (s.parent) {
                if ((<any>s.parent).removeElement != null)
                    (<any>s.parent).removeElement(s);
                else
                    (<any>s.parent).removeChild(s);
                Laya.stage.off(Laya.Event.RESIZE, s, s.resize);
            }
        }
        public clear():void
        {
            let s= this;
            s._commitFunc = null;
        }
        protected resize(): void
        {

        }
        public setData(data: any): void {
        }
        protected createBtn(label: string, color: string = "#000000"): Laya.Button {
            let s = this;
            let btn: Laya.Button;
            btn = new Laya.Button;
            btn.skin = Conf.blueBtn;
            btn.label = label;
            btn.labelColors = "#000000";
            btn.labelSize = 30;
            btn.stateNum = 1;
            btn.on(Laya.Event.CLICK, s, s.oper);
            return btn;
        }
    }
}
